package com.tom.demo.service.approvalsettings.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tom.demo.common.entity.OaProcessType;
import com.tom.demo.common.enumeration.FieldTypeEnum;
import com.tom.demo.common.enumeration.ResponseEnum;
import com.tom.demo.common.exception.CustomException;
import com.tom.demo.common.response.Result;
import com.tom.demo.common.utils.SecurityUtils;
import com.tom.demo.mapper.OaProcessTypeMapper;
import com.tom.demo.service.approvalsettings.OaProcessTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【oa_process_type(审批类型表)】的数据库操作Service实现
* @createDate 2025-03-19 23:33:06
*/
@Slf4j
@Service
public class OaProcessTypeServiceImpl implements OaProcessTypeService {

    @Resource
    private OaProcessTypeMapper oaProcessTypeMapper;

    @Resource
    private Snowflake snowflake;

    @Override
    public Result list(Integer page, Integer pageSize, OaProcessType oaProcessType) {
        PageHelper.startPage(page, pageSize);
        List<OaProcessType> oaProcessTypes = oaProcessTypeMapper.selectByPageList(oaProcessType);
        PageInfo<OaProcessType> pageInfo = new PageInfo<>(oaProcessTypes);
        return Result.success(pageInfo);
    }

    @Override
    public Result insert(OaProcessType oaProcessType) {
        Integer i = oaProcessTypeMapper.selectCount(oaProcessType);
        if(i >0){
            throw  new CustomException(ResponseEnum.OA_PROCESS_TYPE_DATA_EXIST);
        }

        oaProcessType.setId(snowflake.nextIdStr());
        oaProcessType.setCreateBy(SecurityUtils.getLoginUserName());
        oaProcessType.setCreateTime(new Date());
        oaProcessType.setIsDeleted(FieldTypeEnum.IsDelete.NOT_DELETE.getStatus());
        int row = oaProcessTypeMapper.insert(oaProcessType);
        return row >0 ?Result.success():Result.error("添加审批类型失败!");
    }

    @Override
    public Result deleteById(String id) {
        OaProcessType oaProcessType = oaProcessTypeMapper.findById(id);
        oaProcessType.setUpdateTime(new Date());
        oaProcessType.setUpdateBy(SecurityUtils.getLoginUserName());
        oaProcessType.setIsDeleted(FieldTypeEnum.IsDelete.IS_DELETE.getStatus());
        int row = oaProcessTypeMapper.updateById(oaProcessType);
        return row >0 ?Result.success():Result.error("删除审批类型失败!");
    }

    @Override
    public Result updateById(OaProcessType oaProcessType) {
        Integer i = oaProcessTypeMapper.selectCount(oaProcessType);
        if(i >0){
            throw  new CustomException(ResponseEnum.OA_PROCESS_TYPE_DATA_EXIST);
        }
        int row = oaProcessTypeMapper.updateById(oaProcessType);
        return row >0 ?Result.success():Result.error("修改审批类型失败!");
    }

    @Override
    public Result getOptions() {
        List<OaProcessType> oaProcessTypes = oaProcessTypeMapper.getOptions();
        return Result.success(oaProcessTypes);
    }
}




